import { _decorator, Component, Node } from 'cc';
import { ecs } from 'db://oops-framework/libs/ecs/ECS';
import VMParent from 'db://oops-framework/libs/model-view/VMParent';
import { CCVMParentComp } from 'db://oops-framework/module/common/CCVMParentComp';
import { ModuleUtil } from 'db://oops-framework/module/common/ModuleUtil';
import { UIID } from '../../common/config/GameUIConfig';
import { Match } from '../Match';
import { oops } from 'db://oops-framework/core/Oops';
const { ccclass, property } = _decorator;

@ccclass('CreateRoomViewComp')
@ecs.register('CreateRoomView', false)
export class CreateRoomViewComp extends CCVMParentComp {
    data: any = {
        roomName:""
    };
    reset(): void {

    }
    protected start(): void {
        
    }

    async onDoCreateRoom() {
        const res = await this.getEntity<Match>().createRoom(this.data.roomName);
        if (res != null) {
            oops.gui.toast("创建房间成功");
            //加入房间
            const isEnter = await this.getEntity<Match>().joinRoom(res.roomId);
            if (isEnter) {
                this.onClose();
            } else {
                oops.gui.toast("加入房间失败");
            }
            
        } else {
            oops.gui.toast("创建房间失败");
        }
        
    }

    onClose() {
        ModuleUtil.removeViewUi(this.ent, CreateRoomViewComp, UIID.CreateRoom);
    }
}


